# 生成验证码
import random

from xeger import Xeger

bases = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
         'w', 'x', 'y', 'z']


# 使用前请输入：pip install xeger
# 根据正则表达式生成验证码
def generatCheckCodeByXeger(pattern):
    # 默认长度为6
    x = Xeger(limit=10)
    checkCode = x.xeger(pattern)
    return checkCode


# 生成包含大写字母、小写字母、数字
def generatCheckCode():
    code = ''
    while len(code) < 6:
        if random.randint(2, 3) % 2 == 0:
            if random.randint(2, 3) % 2 == 0:
                code += bases[random.randint(0, len(bases) - 1)].upper()
            else:
                code += bases[random.randint(0, len(bases) - 1)].lower()
        else:
            code += str(random.randint(0, 9))
    return code


if __name__ == "__main__":
    # 这是一段邮箱的正则
    pattern = '^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$'
    codes = generatCheckCodeByXeger(pattern)
    print(codes)
    print(generatCheckCode())
